package com.oss.service.system;

import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.beans.factory.annotation.Autowired;

import com.oss.PMF;
import com.oss.model.Link;
import com.oss.model.Project;
import com.oss.service.LinkService;
import com.oss.service.ProjectService;

/**
 * 系统配置，初始化及清理服务
 * 
 * @author xueqiang.mi
 * @since 2010-7-25
 */
public class SystemService {

	@Autowired
	protected ProjectService prjService;

	@Autowired
	protected LinkService lnkService;

	/**
	 * 清除所有Link
	 */
	@SuppressWarnings("unchecked")
	public void clearLinks() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(Link.class);
			query.setRange(0, 500);
			Collection<Link> lnks = (Collection<Link>) query.execute();
			pm.deletePersistentAll(lnks);
		} finally {
			pm.close();
		}
	}

	/**
	 * 清除所有Project
	 * 
	 * @param tagId
	 */
	@SuppressWarnings("unchecked")
	public void clearPrjs() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(Project.class);
			query.setRange(0, 500);
			Collection<Project> prjs = (Collection<Project>) query.execute();
			pm.deletePersistentAll(prjs);
		} finally {
			pm.close();
		}
	}

	/**
	 * 清除一个标签下的所有Project
	 * 
	 * @param tagId
	 */
	@SuppressWarnings("unchecked")
	public void clearPrjsByTag(String tagId) {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(Project.class, "tag == :tagId");
			query.setRange(0, 500);
			Collection<Project> prjs = (Collection<Project>) query.execute(tagId);
			pm.deletePersistentAll(prjs);
		} finally {
			pm.close();
		}
	}

}
